package com.neusoft.thread1safe;
/**
 * 线程的中断
 * @author Zhang.Yan
 *
 */
public class ThreadInterruptTest
{
	
	public static void main(String[] args) throws InterruptedException
	{
		MyThread1 t = new MyThread1("MyThread");
		t.start();
		Thread.sleep(100);// 睡眠100毫秒
		t.interrupt();// 中断t线程
		
	}
	
}

class MyThread extends Thread
{

	int i = 0;
	public MyThread(String name) 
	{
		super(name);
	}
	
	public void run() 
	{
		while(true) 
		{
			// 死循环，等待被中断
			System.out.println(getName() + getId() + "执行了" + ++i + "次");
		}
	}
}


class MyThread1 extends Thread
{

	int i = 0;
	public MyThread1(String name) 
	{
		super(name);
	}
	
	public void run() 
	{
		while(!isInterrupted()) 
		{
			// 死循环，等待被中断
			System.out.println(getName() + getId() + "执行了" + ++i + "次");
		}
	}
}
